home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2c.lha / p4-1.2c / monitors / gridlog.shmem.c < prev    next >
C/C++ Source or Header  |  1993-05-24  |  7KB  |  309 lines

  1. #define ALOG_TRACE
  2. #include "p4.h"
  3. /***** #include  <sys/tmp_ctl.h>  sequent specific *****/
  4.  
  5. #define ASKFOR_WORK     1
  6. #define ASKED           2       
  7. #define START_WORK      3
  8. #define END_WORK        4
  9. #define PUTTING_IN_POOL 5
  10. #define PUT_IN_POOL     6
  11.  
  12. #define ROWS         200
  13. #define COLUMNS     200
  14. struct globmem {
  15.     double a[ROWS+2][COLUMNS+2];
  16.     double b[ROWS+2][COLUMNS+2];
  17.     int st[ROWS+2], pq[ROWS+1];
  18.     int pqbeg, pqend, goal, nproc, rows, columns;
  19.     p4_askfor_monitor_t MO;
  20.     p4_barrier_monitor_t BA;
  21. } *glob;
  22.  
  23. double avggrid();
  24. double avgbnd();
  25.  
  26. slave()
  27. {
  28.     ALOG_SETUP(p4_get_my_id(),ALOG_TRUNCATE);
  29.     work('s');
  30.     ALOG_OUTPUT;
  31. }
  32.  
  33. phi(x,y)            /* The function on the boundary */
  34. int x,y;
  35. {
  36.       return((x * x) - (y * y) + (x * y));   
  37. }
  38.  
  39. main(argc,argv)
  40. int argc;
  41. char **argv;
  42. {
  43.     int i;
  44.     int timestart, timeend;
  45.     double avg;
  46.  
  47.     p4_initenv(&argc,argv);
  48.  
  49.     ALOG_ENABLE;
  50.     ALOG_MASTER(p4_get_my_id(),ALOG_TRUNCATE);
  51.  
  52.     ALOG_DEFINE(ASKFOR_WORK,"Asking","");
  53.     ALOG_DEFINE(ASKED,"Asked","");
  54.     ALOG_DEFINE(START_WORK,"Working","slab %d");
  55.     ALOG_DEFINE(END_WORK,"Finished","slab %d");
  56.     ALOG_DEFINE(PUTTING_IN_POOL,"Putting","slab %d");
  57.     ALOG_DEFINE(PUT_IN_POOL,"Put","slab %d");
  58.  
  59.     glob = (struct globmem *) p4_shmalloc(sizeof(struct globmem));
  60.  
  61.     p4_askfor_init(&(glob->MO));
  62.     p4_barrier_init(&(glob->BA));
  63.  
  64. /*
  65.     printf("enter number of processes: ");
  66.     scanf("%d",&glob->nproc);
  67. */
  68.     printf("enter the number of rows: ");
  69.     scanf("%d",&glob->rows);
  70.     printf("enter the number of columns: ");
  71.     scanf("%d",&glob->columns);
  72.     printf("enter the number of iterations: ");
  73.     scanf("%d",&glob->goal);
  74.  
  75.     gridinit(glob->a,glob->rows,glob->columns);
  76.     gridinit(glob->b,glob->rows,glob->columns);
  77.     
  78.     glob->pqbeg = glob->pqend = 0;
  79.     for (i=1; i <= glob->rows; i++)
  80.         queueprob(i);
  81.  
  82.     /* initialize the status vector */
  83.     for (i=0; i < (glob->rows+2); i++)
  84.         glob->st[i] = 0;
  85.  
  86.     p4_create_procgroup();
  87.     if (p4_get_my_id() != 0)
  88.     {
  89.     slave();
  90.     exit(0);
  91.     }
  92.  
  93.     glob->nproc = p4_num_total_ids();
  94.  
  95.     printf("\nnproc\tgoal\trows\tcolumns\n");
  96.     printf("%d \t  %d \t  %d \t  %d \n",
  97.        glob->nproc,glob->goal,glob->rows,glob->columns);
  98.     timestart = p4_clock();
  99. /*
  100.     for (i=1; i <= glob->nproc-1; i++) {
  101.         p4_create(slave);
  102.     }
  103. */
  104.     work('m');
  105.     timeend = p4_clock();
  106.     printf("total time %.3f seconds\n",(timeend - timestart)/1000.0);
  107.  
  108. /* 
  109.     printf("the resulting grid:\n");
  110.     if (glob->goal % 2 == 0)
  111.         printgrid(glob->a,glob->rows,glob->columns);
  112.     else
  113.         printgrid(glob->b,glob->rows,glob->columns);
  114. */
  115.     if (glob->goal % 2 == 0)
  116.         avg = avggrid(glob->a,glob->rows,glob->columns);
  117.     else
  118.         avg = avggrid(glob->b,glob->rows,glob->columns);
  119.     printf("average value of grid = %f\n",avg);
  120.  
  121.     ALOG_OUTPUT;
  122.     p4_wait_for_end(); 
  123. }
  124.  
  125. /* "m" is the matrix, "r" is the number of rows of data (m[1]-m[r];
  126.    m[0] and m[r+1] are boundaries), and "c" is the number of columns
  127.    of data.
  128. */
  129.  
  130. gridinit(m,r,c)
  131. double m[ROWS+2][COLUMNS+2];
  132. int r, c;
  133. {
  134.     int i, j;
  135.     double bndavg;
  136.     
  137.     for (j=0; j < (c + 2); j++)
  138.     {
  139.         m[0][j] = phi(1,j+1);
  140.         m[r+1][j]= phi(r+2,j+1);
  141.     }
  142.     for (i=1; i < (r + 2); i++)
  143.     {
  144.         m[i][0] = phi(i+1,1);
  145.         m[i][c+1] = phi(i+1,c+2);
  146.     }
  147.     bndavg = avgbnd(m,r,c);
  148.     printf("boundary average = %f\n",bndavg);
  149.  
  150.     /* initialize the interior of the grids to the average over the boundary*/
  151.     for (i=1; i <= r; i++)
  152.         for (j=1; j <= c; j++)
  153.             /* m[i][j] = bndavg; this optimization hinders debugging */
  154.         m[i][j] = 0;
  155. }
  156.  
  157. queueprob(x)
  158. int x;
  159. {
  160.     glob->pq[glob->pqend] = x;
  161.     glob->pqend = (glob->pqend + 1) % (ROWS + 1);
  162. }
  163.  
  164. compute(p,q,r,columns)
  165. double p[ROWS+2][COLUMNS+2];
  166. double q[ROWS+2][COLUMNS+2];
  167. int r;
  168. int columns;
  169. {
  170.     int j;
  171.  
  172.     for (j = 1; j <= columns; j++) 
  173.         q[r][j] = (p[r-1][j] + p[r+1][j] + p[r][j-1] + p[r][j+1]) / 4.0;
  174. }
  175.  
  176. int putprob(r)
  177. int r;
  178. {
  179.     int qprob;
  180.  
  181.     ALOG_LOG(p4_get_my_id(),PUTTING_IN_POOL,r,"");
  182.  
  183.     qprob = FALSE;
  184.     glob->st[r]++;
  185.     if (r == 1)
  186.         glob->st[0] = glob->st[r];
  187.  
  188.     else if (r == glob->rows)
  189.         glob->st[glob->rows+1] = glob->st[r];
  190.  
  191.     if (glob->st[r] < glob->goal)
  192.     {
  193.         if ((r > 1) && (glob->st[r-2] >= glob->st[r]) 
  194.             && (glob->st[r-1] == glob->st[r]))
  195.     {
  196.             queueprob(r-1);
  197.             qprob = TRUE;
  198.         }
  199.         if (r < glob->rows && glob->st[r+1] == glob->st[r] 
  200.                && glob->st[r+1] <= glob->st[r+2])
  201.     {
  202.             queueprob(r+1);
  203.             qprob = TRUE;
  204.         }
  205.         if (glob->st[r-1] == glob->st[r] && 
  206.         glob->st[r] == glob->st[r+1])
  207.     {
  208.             queueprob(r);
  209.             qprob = TRUE;
  210.         }
  211.     }
  212.     ALOG_LOG(p4_get_my_id(),PUT_IN_POOL,qprob,"");
  213.     return(qprob);
  214. }
  215.  
  216. int getprob(v)
  217. int *v;
  218. {
  219.     int rc = 1;
  220.     int *p = (int *) v;
  221.  
  222.     if (glob->pqbeg != glob->pqend) 
  223.     {
  224.     *p = glob->pq[glob->pqbeg];
  225.     glob->pqbeg = (glob->pqbeg+1) % (ROWS + 1); 
  226.     rc = 0;
  227.     }
  228.     return(rc);
  229. }
  230.  
  231. P4VOID reset() 
  232. {
  233. }
  234.  
  235. work(who)            /* main routine for all processes */
  236. char who;
  237. {
  238.     int r,rc,i;
  239.     int my_id = p4_get_my_id();
  240.  
  241.     /***** sequent specific stuff
  242.     printf("old tmp_affinity = %d\n",tmp_affinity(my_id));
  243.     printf("new tmp_affinity = %d\n",tmp_affinity(AFF_QUERY));
  244.     *****/
  245.  
  246.     p4_barrier(&(glob->BA),glob->nproc);
  247.  
  248.     ALOG_LOG(my_id, ASKFOR_WORK, 0, "");
  249.     rc = p4_askfor(&(glob->MO),glob->nproc,getprob,(P4VOID *)&r,reset);
  250.     ALOG_LOG(my_id,ASKED,rc,"");
  251.  
  252.     while (rc == 0) {
  253.     ALOG_LOG(my_id,START_WORK,r,"");
  254.     if ((glob->st[r] % 2) == 0)
  255.         compute(glob->a,glob->b,r,glob->columns);
  256.     else 
  257.         compute(glob->b,glob->a,r,glob->columns);
  258.     ALOG_LOG(my_id,END_WORK,r,"");
  259.  
  260.     p4_update(&(glob->MO),putprob,(P4VOID *) r);
  261.  
  262.     ALOG_LOG(my_id, ASKFOR_WORK, 0, "");
  263.     rc = p4_askfor(&(glob->MO),glob->nproc,getprob,(P4VOID *)&r,reset);
  264.     ALOG_LOG(my_id,ASKED,rc,"");
  265.     }
  266. }
  267.  
  268. printgrid(m,r,c)
  269. double m[ROWS+2][COLUMNS+2];
  270. int r,c;
  271. {
  272.     int i,j;
  273.     for (i = 0; i < (r+2); i++)
  274.     for (j = 0; j < (c+2); j++)
  275.         printf("%3d %3d %10.5f\n",i,j,m[i][j]);
  276. }
  277.  
  278. double avggrid(m,r,c)
  279. double m[ROWS+2][COLUMNS+2];
  280. int r,c;
  281. {
  282.     int i,j;
  283.     double avg = 0;
  284.  
  285.     for (i = 0; i < (r+2); i++)
  286.     for (j = 0; j < (c+2); j++)
  287.         avg += m[i][j];
  288.     return(avg/((r+2)*(c+2)));
  289. }
  290.  
  291. double avgbnd(m,r,c)
  292. double m[ROWS+2][COLUMNS+2];
  293. int r,c;
  294. {
  295.     int i,j;
  296.     double avg = 0;
  297.  
  298.     for (i = 0; i < (r+2); i++)
  299.         avg += m[i][0];
  300.     for (i = 0; i < (r+2); i++)
  301.         avg += m[i][c+1];
  302.     for (i = 1; i < (c+1); i++)
  303.         avg += m[0][i];
  304.     for (i = 1; i < (c+1); i++)
  305.         avg += m[r+1][i];
  306.     return(avg/(2*(c+2) + 2*(r+2) - 4)); /* average over boundary */
  307. }
  308.  
  309.